#!/bin/bash
# fan yang
# Thu Sep  9 20:03:32 CST 2021
# ./init.sh enp1s0 3


function check() {
    if [[ $? == 0 ]]; then
        echo -e "\033[32m${1} --------> success\033[0m";
    else
        echo -e "\033[31m${1} --------> fail\033[0m";
        exit 1;
    fi
}

rm -rf /root/tmp
mkdir -p /root/tmp
check 'mkdir /root/tmp'
cd /root/tmp

# ssh
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
check 'cp sshd_config'

sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
check 'PermitRootLogin'



echo



# timezone set up
echo 'Asia/Shanghai' > /etc/timezone
timedatectl set-timezone Asia/Shanghai
check 'timezone set up'



echo



# sources.list
cat > /etc/apt/sources.list << END
deb http://172.24.0.7/ubuntu/packages main/
deb http://172.24.0.7/ubuntu/packages ros/
END

apt update --allow-insecure-repositories &>/dev/null
check '172.24.0.7 apt update'



echo




# network manage
apt install network-manager -y --allow-unauthenticated &>/dev/null
check 'network-manager install'

cat > /etc/NetworkManager/NetworkManager.conf << END
[main]
plugins=ifupdown,keyfile,ofono
dns=dnsmasq

[ifupdown]
managed=true

[keyfile]
unmanaged-devices=*,except:type:wifi,except:type:gsm,except:type:cdma,except:type:wwan,except:type:ethernet,except:type:vlan
END

cp /etc/network/interfaces /etc/network/interfaces.bak
echo '' > /etc/network/interfaces

systemctl disable networking.service &>/dev/null
check 'disable networking'
systemctl stop networking.service &>/dev/null
check 'stop networking'

systemctl enable NetworkManager
check 'enable NetworkManager'
systemctl restart NetworkManager
check 'restart NetworkManager'

nmcli con add con-name ${1}-static type ethernet ifname ${1} &>/dev/null
check "add ${1}-static"

nmcli con modify ${1}-static \
ipv4.addresses 172.24.${2}.250/16 \
ipv4.gateway 172.24.0.1 \
ipv4.dns 114.114.114.114,8.8.8.8 \
ipv4.method man \
connection.autoconnect yes &>/dev/null
check "modify ${1}-static"

# nmcli con up ${1}-static &>/dev/null
# check "up ${1}-static"

nmcli con add con-name ${3}-static type ethernet ifname ${3} &>/dev/null
check "add ${3}-static"

nmcli con modify ${3}-static \
ipv4.addresses 10.10.10.100/24 \
+ipv4.addresses 192.168.1.100/24 \
+ipv4.addresses 192.168.10.250/24 \
ipv4.method man \
connection.autoconnect yes &>/dev/null
check "modify ${3}-static"

nmcli con up ${3}-static &>/dev/null
check "up ${3}-static"



# lnmp
echo '---------------------------lnmp start------------------------------------'
apt install zip lrzsz gcc g++ make \
-y --allow-unauthenticated &>/dev/null
check 'zip lrzsz gcc g++ make install'

apt  install libpcre3 libpcre3-dev openssl libssl-dev \
-y --allow-unauthenticated &>/dev/null
check 'nginx rely install'

apt install cmake libtirpc-dev libncurses5-dev pkg-config \
-y --allow-unauthenticated &>/dev/null
check 'mysql rely install'

apt install libxml2-dev libxml2 libsqlite3-dev \
libcurl4-openssl-dev libpng-dev \
libjpeg-dev libfreetype6-dev \
-y --allow-unauthenticated &>/dev/null
check 'php rely install'

apt install autoconf automake libtool \
-y --allow-unauthenticated &>/dev/null
check 'oniguruma rely install'

cd /root/tmp
wget http://172.24.0.7/ubuntu/soft/lnmp/oniguruma-6.9.4.tar.gz \
-O oniguruma-6.9.4.tar.gz &>/dev/null
check 'wget oniguruma'

tar xf oniguruma-6.9.4.tar.gz && cd oniguruma-6.9.4
./autogen.sh &>/dev/null
check 'autogen.sh'

./configure --prefix=/usr &>/dev/null
check 'configure oniguruma'

make -j12 &>/dev/null
check 'make'

make install &>/dev/null
check 'make install'

cd /root/tmp
wget http://172.24.0.7/ubuntu/soft/lnmp/lnmp.tar.gz \
-O lnmp.tar.gz &>/dev/null &>/dev/null
check 'wget lnmp'

tar xf lnmp.tar.gz -C /opt/ &>/dev/null
check 'lnmp unzip'


cat > /lib/systemd/system/nginx.service <<END
[Unit]
Description=nginx service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=forking

ExecStart=/opt/lnmp/nginx/sbin/nginx
ExecReload=/opt/lnmp/nginx/sbin/nginx -s reload
ExecStop=/opt/lnmp/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
END
check 'nginx.service'

cat > /lib/systemd/system/mysqld.service <<END
[Unit]
Description=mysql service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=forking

User=mysql
Group=mysql
PIDFile=/opt/lnmp/mysql/run/mysqld.pid
ExecStart=/opt/lnmp/mysql/bin/mysqld --daemonize --pid-file=/opt/lnmp/mysql/run/mysqld.pid

[Install]
WantedBy=multi-user.target
END
check 'mysqld.service'

cat > /lib/systemd/system/phpfpm.service <<END
[Unit]
Description=phpfpm service
After=network.target syslog.target

[Service]
Type=forking
PIDFile=/opt/lnmp/php7.4/var/run/php-fpm.pid
ExecStart=/opt/lnmp/php7.4/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
END
check 'phpfpm.service'

useradd -M -s /sbin/nologin nginx &>/dev/null
setfacl -m u:nginx:rwx -R /opt/lnmp/nginx
setfacl -m d:u:nginx:rwx -R /opt/lnmp/nginx
systemctl enable nginx.service --now &>/dev/null
check 'enable nginx'

useradd -M -s /sbin/nologin mysql &>/dev/null
setfacl -m u:mysql:rwx -R /opt/lnmp/mysql
setfacl -m d:u:mysql:rwx -R /opt/lnmp/mysql
systemctl enable mysqld.service --now &>/dev/null
check 'enable mysqld'

setfacl -m u:nginx:rwx -R /opt/lnmp/php7.4
setfacl -m d:u:nginx:rwx -R /opt/lnmp/php7.4
systemctl enable phpfpm.service --now &>/dev/null
check 'enable phpfpm'

echo '---------------------------lnmp end  ------------------------------------'


echo


# ros
echo '------------------------- --ros start------------------------------------'

apt install ros-kinetic-desktop-full -y --allow-unauthenticated &>/dev/null
check 'ros-kinetic-desktop-full'

echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

apt install python-rosinstall python-rosinstall-generator \
python-wstool build-essential python-pip \
-y --allow-unauthenticated &>/dev/null
check 'Dependencies for building packages'

apt install ros-kinetic-navigation ros-kinetic-robot-upstart  \
-y --allow-unauthenticated &>/dev/null
check 'navigation, upstart'

apt install ros-kinetic-rosbridge-server ros-kinetic-robot-pose-publisher \
-y --allow-unauthenticated &>/dev/null
check 'rosbridge, pose-publisher'

apt install libzbar-dev libgoogle-glog-dev libcholmod3.0.6 \
libcxsparse3.1.4 liblua5.2-dev \
-y --allow-unauthenticated &>/dev/null
check 'dev ...'

sed -i "s/DEFAULT_SOURCES_LIST_URL = .*/DEFAULT_SOURCES_LIST_URL = 'http:\/\/172.24.0.7\/ubuntu\/rosdistro\/rosdep\/sources.list.d\/20-default.list'/" /usr/lib/python2.7/dist-packages/rosdep2/sources_list.py

sed -i "s/DEFAULT_INDEX_URL = .*/DEFAULT_INDEX_URL = 'http:\/\/172.24.0.7\/ubuntu\/rosdistro\/index-v4.yaml'/" /usr/lib/python2.7/dist-packages/rosdistro/__init__.py &>/dev/null

sed -i "s/REP3_TARGETS_URL = .*/REP3_TARGETS_URL = 'http:\/\/172.24.0.7\/ubuntu\/rosdistro\/releases\/targets.yaml'/" /usr/lib/python2.7/dist-packages/rosdep2/rep3.py

mkdir -p /etc/ros/rosdep/sources.list.d/
cat > /etc/ros/rosdep/sources.list.d/20-default.list <<END
# os-specific listings first
yaml http://172.24.0.7/ubuntu/rosdistro/rosdep/osx-homebrew.yaml osx

# generic
yaml http://172.24.0.7/ubuntu/rosdistro/rosdep/base.yaml
yaml http://172.24.0.7/ubuntu/rosdistro/rosdep/python.yaml
yaml http://172.24.0.7/ubuntu/rosdistro/rosdep/ruby.yaml
gbpdistro http://172.24.0.7/ubuntu/rosdistro/releases/fuerte.yaml fuerte

# newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead
END

rosdep update &>/dev/null
check 'rosdep update'

echo '------------------------- --ros end  ------------------------------------'



echo



echo '----------------------------soft start------------------------------------'

cd /root/tmp
# ekho
apt install libsndfile1-dev libespeak-dev pulseaudio libncurses5-dev libmp3lame-dev \
-y --allow-unauthenticated &>/dev/null
check 'ekho rely install'

wget http://172.24.0.7/ubuntu/soft/ekho/ekho.tar.gz -O ekho.tar.gz &>/dev/null
check 'wget ekho'
tar xf ekho.tar.gz && mv ekho /opt/
check 'unzip ekho'


cd /root/tmp
# create udev rules
wget http://172.24.0.7/ubuntu/soft/udev/create_udev_rules.tar.gz &>/dev/null
check 'wget create_udev_rules'
tar xf create_udev_rules.tar.gz &>/dev/null
check 'unzip create_udev_rules'
/root/tmp/create_udev_rules/create_udev_rules.sh &>/dev/null
check 'create_udev_rules'

cd /root/tmp
# webssh
wget http://172.24.0.7/ubuntu/soft/webssh/webssh.zip &>/dev/null
check 'wget webssh'
unzip webssh.zip &>/dev/null
check 'unzip webssh'
mkdir -p /var/www/ && mv webssh /var/www/

echo '----------------------------soft end--------------------------------------'


echo


echo '----------------------------pip start------------------------------------'

mkdir -p /root/.pip
cat > /root/.pip/pip.conf << END
[global]
index-url = http://172.24.0.7/ubuntu/python/pip/simple
[install]
trusted-host = 172.24.0.7
END

pip install \
MySQL-python \
PyMySQL==0.7.2 \
modbus_tk \
pymodbus \
redis mysql-replication==0.22 mysql-python \
crcmod \
pyzbar \
ruamel.yaml==0.16.12 ruamel.yaml.clib==0.2.2 \
pydub \
bson \
-i http://172.24.0.7/ubuntu/python/pip/simple &>/dev/null
check 'MySQL-python,PyMySQL,modbus_tk,redis,crcmod,pyzbar,ruamel,pydub,bson'

rm -rf /usr/lib/python2.7/dist-packages/OpenSSL
rm -rf /usr/lib/python2.7/dist-packages/pyOpenSSL-0.15.1.egg-info
pip install bcrypt==3.1.3 paramiko==2.4.1 \
tornado==4.5.2 \
requests==2.18.4 PyJWT==1.6.4 idna==2.10 \
pyopenssl cryptography==3.3.2 \
-i http://172.24.0.7/ubuntu/python/pip/simple &>/dev/null
check 'pip webssh'

echo '----------------------------pip end------------------------------------'



echo

function path() {
    echo "export PATH=$PATH:/opt/lnmp/mysql/bin:/opt/lamp/php7.4/bin/:/opt/ekho/bin/:/opt/python3.7/bin" >> /etc/profile
    echo "export EKHO_DATA_PATH=/opt/ekho/share/ekho-data/" >> /etc/profile
    . /etc/profile

    echo "pulseaudio --start --log-target=syslog" >> /root/.profile
    . /root/.profile
}

if [[ $4 == 0 ]]; then
    path
fi
